﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataEntity;
using System.Collections.ObjectModel;
using Model;

namespace SWA_View
{
     public partial class RI : Form
    {
         int _ProjectID = 0;
        public ObservableCollection<ChiShuiEntity> ChiShuiEntitys = null;
        public ObservableCollection<SpeedEntity> AngleEntitys = null;
        public ObservableCollection<SpeedEntity> WavePeri = null;
        public ObservableCollection<ExperimentCombinationEntity> Combination = null;
        public LWATExperimentEntity Entity = new LWATExperimentEntity();
        public RI(int ProjectID)
        {
            InitializeComponent();
            _ProjectID = ProjectID;
            CommonModel model = new CommonModel();

            ChiShuiEntitys = model.Query<ChiShuiEntity>("ProjectID =" + _ProjectID + " and ExperimentType='" + ExperimentType.零速横摇不规则波RI.ToString() + "'");
            AngleEntitys = model.Query<SpeedEntity>
                ("ProjectID =" + _ProjectID + " and ExperimentType='" +
                ExperimentType.零速横摇不规则波RI.ToString() + "'" + " and  Type='" + SpeedEntityTyp.角度.ToString() + "'");
            WavePeri = model.Query<SpeedEntity>
                ("ProjectID =" + _ProjectID + " and ExperimentType='" +
                ExperimentType.零速横摇不规则波RI.ToString() + "'" + " and  Type='" + SpeedEntityTyp.波峰波谷.ToString() + "'");
            Entity = model.Query<LWATExperimentEntity>("ProjectID =" + _ProjectID + " and ExperimentType='" + ExperimentType.零速横摇不规则波RI.ToString() + "'").FirstOrDefault();
            if (Entity == null)
                Entity = new LWATExperimentEntity() { ProjectID = _ProjectID, ExperimentType = ExperimentType.零速横摇不规则波RI.ToString() };
            Entity.ChiShuiEntitys = ChiShuiEntitys;
            Entity.SpeedEntitys = AngleEntitys;
            Entity.RateEntitys = WavePeri;

            AngleEntitys.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(SpeedEntitys_CollectionChanged); //(sender1, e1) =>

            ChiShuiEntitys.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ChiShuiEntitys_CollectionChanged);

            WavePeri.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(WavePeri_CollectionChanged);
            WavePeri_CollectionChanged(null, null);
            ChiShuiEntitys_CollectionChanged(null, null);
            SpeedEntitys_CollectionChanged(null, null);
            SetData(Entity);
        }

        private List<int> DelAngleEntitys = new List<int>();

        private List<int> DelWavePeriEntitys = new List<int>();

        private List<int> DelChiShuiEntitys = new List<int>();

        public void SetData(LWATExperimentEntity entiyt)
        {
            txtPaishuiTiji.Text = entiyt.Volume + "";
            txtChuanboZhongxinGaodu.Text = entiyt.Height + "";
            txtChuanboZhongxinZongxiangWeizhi.Text = entiyt.Location;
            txtChuanboZongxiangGuanxingBanjing.Text = entiyt.YSemi + "";
            txtChuanboHengxiangGuanxingBanjing.Text = entiyt.XSemi + "";
        }

        public void GetData()
        {
            Entity.Volume = float.Parse(txtPaishuiTiji.Text);
            Entity.Height = float.Parse(txtChuanboZhongxinGaodu.Text);
            Entity.Location = Entity.Location;
            Entity.YSemi = float.Parse(txtChuanboZongxiangGuanxingBanjing.Text);
            Entity.XSemi = float.Parse(txtChuanboHengxiangGuanxingBanjing.Text);
        }

        private void txtRealLength_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb != null)
            {
                try
                {
                    double.Parse(tb.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("只能输入数字");
                    tb.Focus();
                    tb.Text = "0";
                }
            }
        }

        void WavePeri_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            listViewWaveHP.Items.Clear();
            if (WavePeri.Count > 0)
            {
                ListViewItem item = null;
                foreach (var c in WavePeri)
                {
                    item = new ListViewItem(c.Speed + "");
                    item.Tag = c;
                    //listViewFrequency.Items.Add(item);
                    listViewWaveHP.Items.Add(item);
                }
            }
        }

        void ChiShuiEntitys_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            listViewDraft.Items.Clear();
            if (ChiShuiEntitys.Count > 0)
            {

                ListViewItem item = null;
                foreach (var c in ChiShuiEntitys)
                {
                    item = new ListViewItem(c.ShouChiShui + "");
                    item.Tag = c;
                    item.SubItems.Add(c.WeiChiShui + "");
                    item.SubItems.Add(c.DSC);
                    listViewDraft.Items.Add(item);
                }
            }
        }

        void SpeedEntitys_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            listViewAngle.Items.Clear();
            if (AngleEntitys.Count > 0)
            {
                ListViewItem item = null;
                foreach (var c in AngleEntitys)
                {
                    item = new ListViewItem(c.Speed + "");
                    item.Tag = c;
                    listViewAngle.Items.Add(item);
                }
            }
        }


        private string GetIDStr(List<int> IDs)
        {
            if (IDs.Count > 0)
            {
                string idstr = IDs[0] + " ";
                for (int i = 1; i < IDs.Count; i++)
                {
                    idstr += " ," + IDs[i];
                }
                return idstr;
            }
            return "";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

            DialogResult rsltEnsure = MessageBox.Show("确定要退出吗？", "确认关闭", MessageBoxButtons.OKCancel);
            if (rsltEnsure == DialogResult.OK)
            {
                
                this.DialogResult = DialogResult.Cancel;
                //this.Close();
            }
            else
            {

            }
        }


        private void btnAngle_Click(object sender, EventArgs e)
        {
            AngleForm dlg = new AngleForm(0, 0, DataEntity.ExperimentType.零速横摇规则波RT);
            DialogResult dlgRslt = dlg.ShowDialog(this);
            if (dlgRslt == DialogResult.OK)
            {
                AngleEntitys.Add(dlg.Entity);
            }
        }

        private void btnWaveHP_Click(object sender, EventArgs e)
        {
            WaveHpForm dlg = new WaveHpForm(0, 0, DataEntity.ExperimentType.零速横摇不规则波RI);
            DialogResult dlgRslt = dlg.ShowDialog(this);
            if (dlgRslt == DialogResult.OK)
            {
                WavePeri.Add(dlg.Entity);
            }

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (ChiShuiEntitys.Count <= 0 || AngleEntitys.Count <= 0 || WavePeri.Count <= 0)
            {
                return;
            }
            GetData();
            CommonModel model = new CommonModel();
            if (DelAngleEntitys.Count > 0)
                model.delete("SpeedEntity", " id in (" + GetIDStr(DelAngleEntitys) + ")");
            if (DelWavePeriEntitys.Count > 0)
                model.delete("SpeedEntity", " id in (" + GetIDStr(DelWavePeriEntitys) + ")");
            if (DelChiShuiEntitys.Count > 0)
                model.delete("ChiShuiEnity", " id in (" + GetIDStr(DelChiShuiEntitys) + ")");
            Combination = new ObservableCollection<ExperimentCombinationEntity>();
            model.Save<LWATExperimentEntity>(Entity);
            foreach (var a in ChiShuiEntitys)
            {
                foreach (var b in WavePeri)
                {
                    foreach (var c in AngleEntitys)
                    {
                        ExperimentCombinationEntity entity = null;
                        if (c.ID > 0 && b.ID > 0 && a.ID > 0)
                            entity = model.Query<ExperimentCombinationEntity>("ExperimentType='" + ExperimentType.零速横摇不规则波RI
                                + "' and ProjectID" + _ProjectID + " and DataCombination='" + Entity.Text + a.Text + b.Text + c.Text + "'").FirstOrDefault();
                        if (entity == null)
                            entity = new ExperimentCombinationEntity()
                            {
                                ProjectID = _ProjectID,
                                ExperimentType = ExperimentType.零速横摇不规则波RI.ToString(),
                                DataCombination = Entity.Text + a.Text + b.Text + c.Text,
                                CreateDate = DateTime.Now,
                                IsFinished = "否",
                                IsIng = "否"
                            };
                        Combination.Add(entity);
                        model.Save<SpeedEntity>(c);
                    }
                    model.Save<SpeedEntity>(b);
                }
                model.Save<ChiShuiEntity>(a);
            }
            this.DialogResult = DialogResult.OK;
            
        }

        private void btnDraftDel_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listViewDraft.SelectedItems)
            {
                ChiShuiEntity entity = item.Tag as ChiShuiEntity;
                if (entity != null)
                {
                    ChiShuiEntitys.Remove(entity);
                    if (entity.ID > 0)
                        DelAngleEntitys.Add(entity.ID);
                }

            }
        }

        public void btnDraft_Click(object sender, EventArgs e)
        {
            DraftForm draftDlg = new DraftForm(0, 0, DataEntity.ExperimentType.零速横摇不规则波RI);
            DialogResult rsltDraft = draftDlg.ShowDialog(this);
            if (rsltDraft == DialogResult.OK)
            {
                ChiShuiEntitys.Add(draftDlg.Entity);
            }
        }

        private void btnAngleDel_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listViewAngle.SelectedItems)
            {
                SpeedEntity entity = item.Tag as SpeedEntity;
                if (entity != null)
                {
                    AngleEntitys.Remove(entity);
                    if (entity.ID > 0)
                        DelAngleEntitys.Add(entity.ID);
                }

            }
        }

        private void btnWaveHPDel_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listViewWaveHP.SelectedItems)
            {
                //WavePeri.Remove(item.Tag as SpeedEntity);
                SpeedEntity entity = item.Tag as SpeedEntity;
                if (entity != null)
                {
                    WavePeri.Remove(entity);
                    if (entity.ID > 0)
                        DelAngleEntitys.Add(entity.ID);
                }

            }
        }

        private void RI_Load(object sender, EventArgs e)
        {
            
        }
    }
}
